//
//  Home1TableViewCell.m
//  MobileOffice
//
//  Created by Appel on 16/10/17.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "Home1TableViewCell.h"

@implementation Home1TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor=[UIColor clearColor];
        self.backgroundColor=[UIColor clearColor];
        _timeLabel=[MyControl creatLabelWithFrame:CGRectMake(15, 8, 40, 20) text:@"0"];
        _timeLabel.font=FONT_13;
        _timeLabel.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:_timeLabel];
        
        _lineImageView=[MyControl creatImageViewWithFrame:CGRectMake(_timeLabel.x+_timeLabel.width+8, 0, 0.5, 66) imageName:@""];
        _lineImageView.backgroundColor=[UIColor lightGrayColor];
        [self.contentView addSubview:_lineImageView];
        
        _stateImageView=[MyControl creatImageViewWithFrame:CGRectMake(_timeLabel.x+_timeLabel.width, 10, 16, 16) imageName:@"checkbox_normal@2x"];
        [self.contentView addSubview:_stateImageView];
        
        _backButton=[MyControl creatButtonWithFrame:CGRectMake(_lineImageView.x+_lineImageView.width+15, 8, IPHONE_WIDTH-(_lineImageView.x+_lineImageView.width+15)-8, 50) target:self sel:@selector(buttonClick) tag:100 image:nil title:@""];
        _backButton.backgroundColor=[UIColor lightGrayColor];
        _backButton.layer.cornerRadius=5;
        [self.contentView addSubview:_backButton];
        
        
        _titleLabel=[MyControl creatLabelWithFrame:CGRectMake(5, 5, _backButton.width-45, 15) text:@""];
        _titleLabel.font=FONT_14;
        [_backButton addSubview:_titleLabel];
        
        _signLabel=[MyControl creatLabelWithFrame:CGRectMake(_titleLabel.x+_titleLabel.width, _titleLabel.y, 15, 15) text:@""];
        _signLabel.backgroundColor=[UIColor redColor];
        _signLabel.textColor=[UIColor whiteColor];
        _signLabel.layer.masksToBounds=YES;
        _signLabel.layer.cornerRadius=_signLabel.width/2;
        _signLabel.hidden=YES;
        _signLabel.font=FONT_11;
        _signLabel.textAlignment=NSTextAlignmentCenter;
        [_backButton addSubview:_signLabel];
        
        _signInDetailButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _signInDetailButton.frame=CGRectMake(0, 0, 60, _backButton.height);
        _signInDetailButton.userInteractionEnabled=NO;
        [_signInDetailButton addTarget:self action:@selector(detailClick) forControlEvents:UIControlEventTouchUpInside];
        [_backButton addSubview:_signInDetailButton];
        
        UILabel *typeLabel=[MyControl creatLabelWithFrame:CGRectMake(5, _titleLabel.y+_titleLabel.height+5, 32, 20) text:@"状态:"];
        typeLabel.font=FONT_12;
        typeLabel.textColor=[UIColor grayColor];
        [_backButton addSubview:typeLabel];
        
        _stateLabel=[MyControl creatLabelWithFrame:CGRectMake(typeLabel.x+typeLabel.width, _titleLabel.y+_titleLabel.height+5, _titleLabel.width-(typeLabel.x+typeLabel.width), 20) text:@""];
        _stateLabel.font=FONT_12;
        _stateLabel.textColor=[UIColor grayColor];
        [_backButton addSubview:_stateLabel];
        
        _toExplainButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _toExplainButton.frame=CGRectZero;
        _toExplainButton.layer.masksToBounds=YES;
        _toExplainButton.layer.cornerRadius=5;
        _toExplainButton.hidden=YES;
        _toExplainButton.titleLabel.font=FONT_13;
        [_toExplainButton addTarget:self action:@selector(explainClick) forControlEvents:UIControlEventTouchUpInside];
        [_backButton addSubview:_toExplainButton];
        
        _photoImageView=[[MyImageView alloc]initWithFrame:CGRectMake(_backButton.width-45, 5, 35, _backButton.height-10)];
        //_photoImageView.image=[UIImage imageNamed:@"k_ic_1wifi@2x"];
        [_backButton addSubview:_photoImageView];
        
//        _beginTimeLabel=[MyControl creatLabelWithFrame:CGRectMake(_backButton.width-100, _titleLabel.y, 100, 20) text:@""];
//        _beginTimeLabel.font=FONT_11;
//        _beginTimeLabel.textColor=[UIColor grayColor];
//        _beginTimeLabel.textAlignment=NSTextAlignmentCenter;
//        [_backButton addSubview:_beginTimeLabel];
    }
    return self;
}

- (void)setCellInfoWithModel:(SignInInfoModel *)model{
    self.model=model;
    _signLabel.hidden=YES;//先隐藏掉
    _titleLabel.text=model.result;
    //计算宽度
    _titleLabel.width=[LZXHelper textWidthFromTextString:_titleLabel.text Height:_titleLabel.height fontSize:14]+3;
    _signLabel.x=_titleLabel.x+_titleLabel.width;
    
    _stateLabel.text=model.eventCode;
    _stateLabel.width=[LZXHelper textWidthFromTextString:_stateLabel.text Height:_stateLabel.height fontSize:12]+3;
    //填充数据
    _timeLabel.text=[LZXHelper getTimeStrWithOldDateFomatter:@"yyyy-MM-dd HH:mm" DateStr:model.eventTime newFormatter:@"HH:mm"];
    if (_timeLabel.text.length==0) {
           _timeLabel.text=[LZXHelper getTimeStrWithOldDateFomatter:@"yyyy-MM-dd HH:mm:ss" DateStr:model.eventTime newFormatter:@"HH:mm"]; 
    }
    self.toExplainButton.frame=CGRectMake(_stateLabel.x+_stateLabel.width+5, _stateLabel.y, 50, _stateLabel.height);

    NSInteger exceptionStatus=0;
    if (model.eventStatus!=nil&&model.eventStatus.length>0) {
        //
        NSArray *infoArr=[model.eventStatus componentsSeparatedByString:@"|"];
        if (infoArr.count==2) {
            //取出状态
            exceptionStatus=[infoArr[0] integerValue];
            _signLabel.text=infoArr[1];
            if ([_signLabel.text integerValue]>0) {
                _signLabel.hidden=NO;
                _signInDetailButton.userInteractionEnabled=YES;
            }else{
                _signLabel.hidden=YES;
                _signInDetailButton.userInteractionEnabled=NO;
            }
        }
        
    }
    switch (exceptionStatus) {
        case 0: // 判断状态字体
            if (![model.eventCode isEqualToString:@"正常"]) {
                [self.toExplainButton setTitle:@"去说明" forState:UIControlStateNormal];
                self.stateLabel.textColor=COLOR_PINK;
                self.toExplainButton.backgroundColor=[LZXHelper HexColorToRedGreenBlue:COLOR_BUTTON_RED];
                self.toExplainButton.hidden=NO;
            } else { // 无需说明
                self.stateLabel.textColor=[UIColor grayColor];
                self.toExplainButton.hidden=YES;
            }
            
            break;
        case 1: // 未说明红色背景
            [self.toExplainButton setTitle:@"去说明" forState:UIControlStateNormal];
            self.stateLabel.textColor=[UIColor redColor];
            self.toExplainButton.backgroundColor=[LZXHelper HexColorToRedGreenBlue:COLOR_BUTTON_RED];
            self.toExplainButton.hidden=NO;
            break;
        case 2: // 已说明未审批
            [self.toExplainButton setTitle:@"已说明" forState:UIControlStateNormal];
            self.stateLabel.textColor=[UIColor redColor];
            self.toExplainButton.backgroundColor=[LZXHelper HexColorToRedGreenBlue:COLOR_BUTTON_YELLOW];
            self.toExplainButton.hidden=NO;

            break;
        case 3: // 已说明已审批
            [self.toExplainButton setTitle:@"已说明" forState:UIControlStateNormal];
            self.stateLabel.textColor=[UIColor grayColor];
            self.toExplainButton.backgroundColor=[LZXHelper HexColorToRedGreenBlue:COLOR_BUTTON_GREEN];
            self.toExplainButton.hidden=NO;
            break;
            
        default:
            break;
				}
    //如果eventcode状态为正常
        if ([model.eventCode isEqualToString:@"正常"]) {
            self.stateLabel.textColor=[UIColor grayColor];
        }
}

//去说明
- (void)explainClick{
    if (self.myBlock) {
        self.myBlock(self.model);
    }
}
//签到详情点击
- (void)detailClick{
    if (self.detailBlock) {
        self.detailBlock();
    }
}

- (void)buttonClick{
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
